﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;


public partial class admin_newsletter_editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            using (new SessionScope())
            {
                if (!IsPostBack)
                {
                    litPos.Text = "<a href='/admin/newsletter/default.aspx'>Mẫu newsletter</a><img src='/images/bul4.png'>";
                    int id = ConvertUtility.ToInt32(Request.QueryString["id"]);

                    if (id > 0)
                    {
                        hidID.Value = id.ToString();
                        initForm(true);
                    }
                    else
                    {
                        initForm(false);
                    }
                }
            }

            fail.Visible = false;
        }
        catch (Exception ex) { }
    }
    protected void initForm(bool isEdit)
    {
        if (isEdit)
        {
            btnAdd.Visible = false;
            btnEdit.Visible = true;
            litPos.Text += "Sửa ";
            fillData();
        }
        else
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            litPos.Text += "Thêm mới  ";
        }
    }


    protected void fillData()
    {
        try
        {
            Newsletter_Info newslet = Newsletter_Info.Find(ConvertUtility.ToInt32(hidID.Value));
            txtTitle.Text = newslet.subject;
            txtNote.Text = newslet.chuthich;
            radNewsContent.Value = newslet.noidung;
        }
        catch (Exception ex) { }
    }
    protected Newsletter_Info getNewsLetterInfo(Newsletter_Info newsletter)
    {
        newsletter.subject = txtTitle.Text;
        newsletter.noidung = radNewsContent.Value;
        newsletter.chuthich = txtNote.Text;
        newsletter.createDate = DateTime.Now;
        return newsletter;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Newsletter_Info newsletter = getNewsLetterInfo(new Newsletter_Info());
            using (new SessionScope())
                newsletter.Create();
            Response.Redirect("/admin/newsletter/template.aspx");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Newsletter_Info newsletter = getNewsLetterInfo(Newsletter_Info.Find(ConvertUtility.ToInt32(hidID.Value)));
            using (new SessionScope())
                newsletter.SaveAndFlush();
            Response.Redirect("/admin/newsletter/template.aspx");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidID.Value != "")
            fillData();
        else
            ControlUtility.EmptyTextBoxes(this);

    }
}